var e = getApp(),
    o = require("../../utils/core.js");

Page({
    data: {
        shop_logo: "",
        shop_name: "",
        items: 0,
        is: true,
        name: '',
        headPortrait: '',
        showMask: false,
        openid: '',
        sessionKey: '',
        n:''
    },
    onLoad: function (e) {
        var n = this;
        o.get("wxAppSetting", {}, function (e) {
            var o = e.sysset;
            n.setData({
                shop_logo: o.shoplogo,
                shop_name: o.shopname
            });
        });
    },
    checkboxChange(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value)
        const items = this.data.items
        const values = e.detail.value
        console.log(values.length);
        this.setData({
            items: values.length
        })
    },

    // 上传 用户信息
    uploadUserInfo() {
        let that = this
        let n = that.data.n
        if (that.data.name == '') return wx.showToast({
            title: '请填写昵称',
            icon: 'none'
        })
        if (that.data.headPortrait == '') return wx.showToast({
            title: '请选择头像',
            icon: 'none'
        })
        o.get("wxapp/newauth", {
            nickName: that.data.name,
            avatarUrl: that.data.headPortrait,
            openids: that.data.openid,
            sessionKey: that.data.session_key
        }, function (s) {
            console.log(s,'ssss')
            console.log(that.data.openid,'openid')
            wx.setStorageSync('userinfo', s)
            wx.setStorageSync('openid', that.data.openid)
            e.setCache("userinfo_openid", that.data.openid)
            console.log([3, s]), 1 == s.isblack && wx.showModal({
                title: "无法访问",
                content: "您在商城的黑名单中，无权访问！",
                success: function (o) {
                    o.confirm && e.close(), o.cancel && e.close();
                }
            }), n.userInfo.openid = s.openid, n.userInfo.id = s.id, n.userInfo.uniacid = s.uniacid;
            var t = e.setCache("userinfo", n.userInfo),
                a = e.setCache("userinfo_openid", n.userInfo.openid),
                c = e.setCache("userinfo_id", s.id);
            var l = e.setCache("login_session_key", that.data.session_key);
            wx.showToast({
                title: '登录成功',
            })
            0 != t && 0 != a && 0 != c && 0 != l || o.alert("获取用户信息失败!"), console.log(e.getCache("login_session_key"))
            console.log(s.is_update,'update是什么')
            if (s.is_update == 1) {
                // that.setData({
                //     showMask: true
                // })
            } else {
                e.getSet()
                e.scanCarts()
                wx.showToast({
                    title: '登录成功',
                })
                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }, 2000)
            }
        });
        // o.get('wxapp/setprofile', {
        //     avatar: that.data.headPortrait,
        //     nickname: that.data.name
        // }, function (res) {
        //     console.log(res)
        //     let userinfo = e.getCache("userinfo")
        //     userinfo.avatarUrl = that.data.headPortrait
        //     userinfo.nickName = that.data.name
        //     userinfo.openid = res.openId
        //     userinfo.id = res.id
        //     userinfo.uniacid = res.uniacid;
        //     console.log(222222, userinfo);
        //     e.setCache("userinfo", userinfo)
        //     wx.setStorageSync('userinfo', res)
        //     that.maskBtn()
        // })

    },



    // 获取头像
    onChooseAvatar(e) {
        console.log(e)
        let that = this
        that.setData({
            headPortrait: e.detail.avatarUrl
        })
        wx.uploadFile({
            url: 'https://zzsl.sctqmt.com/app/ewei_shopv2_api.php?i=1&r=util.uploader.upload',
            filePath: e.detail.avatarUrl,
            name: 'file',
            success: function (res) {
                console.log(res)
                console.log(JSON.parse(res.data))
                that.setData({
                    headPortrait: JSON.parse(res.data).files[0].url
                })
                console.log(that.data.headPortrait)
            }
        })
    },

    // 获取名称
    getName(e) {
        this.setData({
            name: e.detail.value
        })
    },



    maskBtn() {
        this.setData({
            showMask: false
        })
        // 关闭后处理问题
        e.getSet()
        e.scanCarts()
        
        
    },

    fwxy() {
        wx.showModal({
            title: '服务协议',
            content: '服务协议内容',
            success: function (res) {
                if (res.confirm) {
                    console.log('点击确认回调')
                } else {
                    console.log('点击取消回调')
                }
            }
        })
    },
    ystk() {
        wx.showModal({
            title: '隐私条款',
            content: '隐私条款内容',
            success: function (res) {
                if (res.confirm) {
                    console.log('点击确认回调')
                } else {
                    console.log('点击取消回调')
                }
            }
        })
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {
        wx.switchTab({
            url: '/pages/index/index',
        })

        // wx.setStorageSync('is', this.data.is)
    },
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {},
    bindGetUserInfo: function () {
        if (this.data.items == 0) {
            wx.showToast({
                title: '请同意用户协议和隐私条款',
                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                duration: 2000
            })
            return
        }
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (n) => {
                let that = this
                console.log(n,'n是什么')
                that.data.n = n
                if (n.userInfo) {
                    wx.login({
                        success: function (i) {
                            console.log([1, i]), o.post("wxapp/login", {
                                code: i.code
                            }, function (i) {
                                var openid = i.openid;
                                console.log(i,'登录')
                                that.setData({
                                    openid: i.openid,
                                    sessionKey: i.session_key
                                })
                                console.log([2, i]), i.error ? o.alert("获取用户登录态失败:" + i.message) : that.setData({
                                    showMask: true
                                })
                                // wx.setStorageSync('openid', openid)
                                // e.setCache("userinfo_openid", i.openid)
                                // console.log([2, i]), i.error ? o.alert("获取用户登录态失败:" + i.message) : o.get("wxapp/newauth", {
                                //     nickName: n.userInfo.nickName,
                                //     avatarUrl: n.userInfo.avatarUrl,
                                //     gender: n.userInfo.gender,
                                //     openids: openid,
                                //     sessionKey: i.session_key
                                // }, function (s) {
                                //     wx.setStorageSync('userinfo', s)
                                //     console.log(333333333333, openid)
                                //     console.log([3, s]), 1 == s.isblack && wx.showModal({
                                //         title: "无法访问",
                                //         content: "您在商城的黑名单中，无权访问！",
                                //         success: function (o) {
                                //             o.confirm && e.close(), o.cancel && e.close();
                                //         }
                                //     }), n.userInfo.openid = s.openid, n.userInfo.id = s.id, n.userInfo.uniacid = s.uniacid;
                                //     console.log(4444444444444, s)
                                //     console.log(n)
                                //     var t = e.setCache("userinfo", n.userInfo),
                                //         a = e.setCache("userinfo_openid", n.userInfo.openid),
                                //         c = e.setCache("userinfo_id", s.id);
                                //     console.log(e.getCache("userinfo"))
                                //     console.log(e.getCache("userinfo_openid")),
                                //         console.log(e.getCache("userinfo_id"));
                                //     var l = e.setCache("login_session_key", i.session_key);
                                //     0 != t && 0 != a && 0 != c && 0 != l || o.alert("获取用户信息失败!"), console.log(e.getCache("login_session_key"))
                                //     if (s.is_update == 1) {
                                //         that.setData({
                                //             showMask: true
                                //         })
                                //     } else {
                                //         e.getSet()
                                //         e.scanCarts()
                                //         wx.showToast({
                                //             title: '登录成功',
                                //         })
                                //         setTimeout(() => {
                                //             wx.switchTab({
                                //                 url: '/pages/index/index',
                                //             })
                                //         }, 2000)
                                //     }
                                // });
                            });
                        },
                        fail: function () {
                            o.alert("获取用户信息失败!");
                        },
                        complete: function () {
                            wx.hideLoading();
                        }
                    });
                }
            }
        })
    },
    // navigateBack: function () {
    //     wx.switchTab({
    //         url: '/pages/index/index',
    //     })
    // },
    close: function () {
        wx.switchTab({
            url: '/pages/index/index',
        })
    }
});